package com.shen.accountbook.module.home.main;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.shen.accountbook.R;
import com.shen.accountbook.module.base.EmptyPresenterImpl;
import com.shen.accountbook.module.base.EmptyView;
import com.shen.accountbook.module.base.MVPActivity;
import com.shen.accountbook.module.home.accountfragment.AccountFragment;
import com.shen.accountbook.module.home.detailedfragment.DetailFragment;
import com.shen.accountbook.module.home.reportformsfragment.ReportFormsFragment;
import com.shen.accountbook.module.home.reportformsfragment.ReportFormsFragmentTemp;
import com.shen.accountbook.module.home.sortmanagefragment.SortManageFragment;
import com.shen.accountbook.widget.BaseBottomBar;

import butterknife.BindView;

/**
 * author:  shen
 * date:    2018/8/24
 */
public class HomeActivity extends MVPActivity<HomePresenterImpl> implements HomeView, BaseBottomBar.OnBottomBarListener{

    /** 标题名 */
    @BindView(R.id.tv_title_name)
    TextView mTvTitleName;
    /** 标题右边的图标 */
    @BindView(R.id.iv_title_right)
    ImageView mIvTitleRight;
    /** tab对应的布局 */
    @BindView(R.id.fl_container)
    FrameLayout mFlContainer;
    /** 底部的布局 -- 导航 */
    @BindView(R.id.home_bottom_bar)
    BaseBottomBar mBottomBar;

    /** 当前选中的tab位置 */
    private int mIndex = 0;
    private FragmentManager mFragmentManager;
    private Boolean mCanClickBoolean = true;
    /** 能够响应点击事件的时间间隔为0.2s */
    private int MSG_CLICK_DURATION = 200;
    /** 标识 */
    private static final int MSG_CLICK = 20180827;


    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (mHandler == null) {
                return;
            }
            int msgNum = msg.what;
            switch (msgNum) {
                case MSG_CLICK:
                    mCanClickBoolean = true;
                    break;
            }
        }
    };

    @Override
    protected HomePresenterImpl createPresenter(Context context) {
        return new HomePresenterImpl(context, this);
    }

    @Override
    protected int getResId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initData() {
        mFragmentManager = getSupportFragmentManager();
        mBottomBar.setOnBottomBarListener(this);
        mBottomBar.showTab(0);
    }

    @Override
    protected void initEvent() {
    }

    /* BaseBottomBar.OnBottomBarListener */
    @Override
    public void showFragment(int index) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(String.valueOf(index));
        if (fragment == null) {
            if (index == 0) {
                fragment = new DetailFragment();
                //fragment = new DetailFragmentTemp();
            } else if (index == 1) {
                // fragment = new ReportFormsFragment();
                fragment = new ReportFormsFragmentTemp();
            } else if (index == 2) {
                fragment = new AccountFragment();
            } else if (index == 3) {
                fragment = new SortManageFragment();
            }

            if (!fragment.isAdded()) {
                obtainFragmentTransaction(index).add(R.id.fl_container, fragment,
                        String.valueOf(index)).commitAllowingStateLoss();
            }
        }
        if (fragment != null) {
            obtainFragmentTransaction(index).show(fragment).commitAllowingStateLoss();
        }

        mIndex = index;
    }

    @Override
    public void hideFragment(int lastIndex, int curIndex) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(String.valueOf(mIndex));
        if (fragment != null) {
            obtainFragmentTransaction(curIndex).hide(fragment).commitAllowingStateLoss();
        }
    }

    @Override
    public void refreshView(int index) {
        if (index == 0) {
        } else if (index == 1) {
        } else if (index == 2) {
        } else if (index == 3) {
        }
    }

    @Override
    public void onTabClick(View view) {
        if (!mCanClickBoolean) {
            return;
        }
        mCanClickBoolean = false;
        mHandler.removeMessages(MSG_CLICK);
        mHandler.sendEmptyMessageDelayed(MSG_CLICK, MSG_CLICK_DURATION);

        mBottomBar.setCurrentView(view);

        if (mIndex == 0) {
            mTvTitleName.setText("记账");
        } else if (mIndex == 1) {
            mTvTitleName.setText("类别报表");
        } else if (mIndex == 2) {
            mTvTitleName.setText("账户");
        } else if (mIndex == 3) {
            mTvTitleName.setText("分类管理");
        }

    }


    /**
     * 获取一个带动画的FragmentTransaction
     *
     * @param index
     * @return
     */
    private FragmentTransaction obtainFragmentTransaction(int index) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        // 设置切换动画
        if (index > this.mIndex) {
            ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left);
        } else {
            ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right);
        }
        return ft;
    }

}
